package com.atguigu.edu.controller.front;

import com.atguigu.edu.entity.Course;
import com.atguigu.edu.entity.Teacher;
import com.atguigu.edu.service.CourseService;
import com.atguigu.edu.service.TeacherService;
import com.atguigu.utils.R;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@CrossOrigin
@Api(description = "(名师接口) 前台端口-8001")
@RequestMapping("/edu/front")
public class TeacherFrontController {

    @Resource
    private CourseService courseService;

    @Resource
    private TeacherService teacherService;

    /**
     * @param current
     * @return
     */
    @ApiOperation("front前台获取名师加分页 无规则前8个")
    @GetMapping("/getTeacherByLimit/{current}/{limit}")
    public R getTeacherByLimit8(@PathVariable long current, @PathVariable long limit) {
        Page<Teacher> page1 = new Page<>(current, limit);
        IPage<Teacher> page = teacherService.page(page1, null);

        long current1 = page1.getCurrent();
        long pages = page1.getPages();
        long size = page1.getSize();
        long total = page1.getTotal();

        boolean hasPrevious = page1.hasPrevious();
        boolean hasNext = page1.hasNext();
        List<Teacher> records = page.getRecords();

        return R.ok().data("teacherRecords", records).data("current", current1).data("total", total).data("pages", pages).data("hasPrevious", hasPrevious).data("hasNext", hasNext);
    }

    @ApiOperation("根据讲师id查询讲师详情及所讲课程")
    @GetMapping("/getTeacherAndCourse/{id}")
    public R getTeacherAndCourse(@PathVariable String id) {
        if (StringUtils.isEmpty(id)) {
            return R.error();
        }
        Teacher teacher = teacherService.getById(id);
        QueryWrapper<Course> wrapper = new QueryWrapper<>();
        wrapper.eq("teacher_id", id);
        List<Course> courseList = courseService.list(wrapper);

        return R.ok().data("teacher", teacher).data("courseList", courseList);
    }
}
